<template>
  <div class="edit-password">
    <el-dialog
      :title="updateParams.grade != null ? '修改成绩' : '录入成绩'"
      center
      :visible.sync="dialogFormVisible"
      @close="resetForm('ruleForm')"
      top="5vh"
      width="500px"
    >
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        size="small"
        label-width="80px"
        class="demo-ruleForm"
        style="width: 400px; margin: 0 auto;"
      >
        <el-form-item label="ID" prop="user_id" ref="user_id">
          <el-input
            v-model.number="ruleForm.user_id"
            disabled
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="user_name">
          <el-input
            v-model="ruleForm.user_name"
            disabled
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="课程号" prop="course_id">
          <el-input
            v-model="ruleForm.course_id"
            disabled
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="课程名" prop="course_name">
          <el-input
            v-model="ruleForm.course_name"
            disabled
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="成绩" prop="grade">
          <el-input
            v-model.number="ruleForm.grade"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { electiveInfo, electiveEdit } from '@/api/elective'
export default {
  name: 'updateUser-pop',
  data() {
    const validateGrade = (rule, value, callback) => {
      console.log(value !== '')
      console.log(Number(value) >= 0 && Number(value) <= 100)
      if (value !== '' && Number(value) >= 0 && Number(value) <= 100) {
        console.log(1212)
        callback()
      } else {
        callback(new Error('成绩范围在0~100之间'))
      }
    }
    return {
      dialogFormVisible: false,
      ruleForm: {
        grade: ''
      },
      rules: {
        grade: [
          {
            validator: validateGrade,
            trigger: 'blur'
          }
        ]
      },
      updateParams: {}
    }
  },

  methods: {
    // 1.打开弹窗
    openMask(record) {
      console.log(record);
      this.updateParams = record
      if (record.type === 'edit') {
        this.userDetail(record)
      }
      this.dialogFormVisible = true
    },

    // 2.根据用户id获取用户详情
    userDetail(record) {
      let params = {
        user_id: record.user_id,
        course_id: record.course_id
      }
      electiveInfo(params)
        .then(res => {
          if (res.result) {
            this.ruleForm = { ...res.data }
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },

    // 4.注册/修改用户
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          electiveEdit(this.ruleForm)
            .then(res => {
              if (res.result) {
                this.resetForm('ruleForm')
                this.$message.success(res.message)
                this.$emit('ok')
              } else {
                this.$message.error(res.message)
              }
            })
            .catch(err => {
              this.$message.error(err.message)
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.dialogFormVisible = false
    }
  }
}
</script>

<style lang="stylus" scoped>
.el-select {
  width 320px
}
</style>
